﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Windows;
using System.Diagnostics;
using word = Microsoft.Office.Interop.Word;
using excel = Microsoft.Office.Interop.Excel;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Reflection;

namespace DocManagerControls.CLASSI
{
    public class DocUtils
    {
        public DocUtils()
        {            
        }


      public static byte[] ReadFileFromPath(string sPath)
        {
            if (string.IsNullOrEmpty(sPath)) return null;

            //Initialize byte array with a null value initially.
            byte[] data = null;

            //Use FileInfo object to get file size.
            FileInfo fInfo = new FileInfo(sPath);
            long numBytes = fInfo.Length;

            //Open FileStream to read file
            using (var fStream = new FileStream(sPath, FileMode.Open,
                                                    FileAccess.Read))
            {

                //Use BinaryReader to read file stream into byte array.
                BinaryReader br = new BinaryReader(fStream);

                //When you use BinaryReader, you need to 

                //supply number of bytes to read from file.
                //In this case we want to read entire file. 

                //So supplying total number of bytes.
                data = br.ReadBytes((int)numBytes);

                br.Close();
                br.Dispose(); 
            }
            
            return data;


        }//fM
        
      public static void SaveBytesInFile(string sPath, byte[] pData)
      {
          if (string.IsNullOrEmpty(sPath)) return ;
          if (pData == null || pData.Length ==0) return ;

          using (var fStream = File.Create(sPath, pData.Length, FileOptions.None))
          {
              var br = new BinaryWriter(fStream);
              br.Write(pData);
          }

      }//fM

      public static void showDoc(string pFilePath)
      {
          if (string.IsNullOrEmpty(pFilePath)) return;
          if (!File.Exists(pFilePath)) return;

          var ext = System.IO.Path.GetExtension( pFilePath.Trim());
                    
            var vbb = new Controls.ViewBox();

            if (ext == ".pdf") showPDF(pFilePath);

            if (ext == ".txt")
            {
                vbb.Messaggio = File.ReadAllText(pFilePath);
                vbb.ShowDialog();
            }

            if (ext == ".doc" || ext==".docx")
            {
                word.Application wordApplication = new word.Application();
                wordApplication.Documents.Open(pFilePath, ReadOnly: false);
                wordApplication.Visible = true; 
            }

            if (ext == ".xls" || ext == ".xlsx")
            {
                var excelApplication = new excel.Application();
                excelApplication.IgnoreRemoteRequests = true;
                excelApplication.Workbooks.Open(pFilePath,ReadOnly:false);  
                excelApplication.Visible = true;               
                
            }
                

      }//fM

      public static void showPDF(string pFile)
      {
          Process process = new Process();
          process.StartInfo.Verb = "Open"; //"PrintTo";

          process.StartInfo.FileName = @"C:\Program Files\Adobe\Reader 9.0\Reader\acrord32.exe";

          process.StartInfo.Arguments = string.Format(" \"{0}\" ", pFile); //   /t

          process.StartInfo.RedirectStandardOutput = false;
          process.StartInfo.CreateNoWindow = true;

          process.StartInfo.UseShellExecute = false;

          process.Start();

          //process.StandardOutput.ReadToEnd();

          process.WaitForExit();

      }//fm
        
      public byte[] GetImageFromDatabase()
      {
          byte[] arrBytes = null ;

          try
          {


              return arrBytes; 
          }
          catch (Exception ex)
          {
              throw ex;
          }
          finally
          {

              
          }         

      }//fM

      public static object clonaOggetto(object pObjToCLone)
      {          
        // Create a memory stream and a formatter.
        MemoryStream ms = new MemoryStream(1000);
        BinaryFormatter bf = new BinaryFormatter(null, 
        new StreamingContext(StreamingContextStates.Clone));
        // Serialize the object into the stream.
        bf.Serialize(ms, pObjToCLone);
        // Position streem pointer back to first byte.
        ms.Seek(0, SeekOrigin.Begin);
        // Deserialize into another object.
        object res = bf.Deserialize(ms);
        // release memory and return the object
        ms.Close();
        return res;
        
      }//fm

      public static TRet Clone<TRet, TBase>(TBase obj) where TRet : TBase, new()
      {
          TRet ret = new TRet();
          PropertyInfo[] properties = typeof(TBase).GetProperties(
              System.Reflection.BindingFlags.Instance |
              System.Reflection.BindingFlags.Public
          );
          foreach (PropertyInfo property in properties)
          {
              object value = property.GetValue(obj, null);
              property.SetValue(ret, value, null);
          }
          return ret;
      }
        


    }//fC
}
